<?php

	require_once(APPPATH . 'controllers/base_controllers/base_validation_controller.php');
	
	class Swamivisits_validation extends Base_validation_controller
	{
		
		public function __construct()
		{
			parent::__construct();
			
			$this->load->library('validation');
		}
		
		public function validate_registration( $eventID )
		{
			switch ($eventID)
			{
				case 1:
					$rules['lastname'] = "trim|required|max_length[16]";
					$rules['firstname'] = "trim|required|max_length[16]";
					$rules['email'] = "trim|required|valid_email|matches[emailConfirm]|max_length[127]";
					$rules['numreg'] = "trim|required|min_length[1]|max_length[2]";
		
					
					$this->validation->set_rules($rules);	
					$validationPassed = $this->validation->run();
					
					if($validationPassed)
					{
						echo( 'VALIDATION_SUCCESS' );
					}
					else
					{
						echo( $this->clean_validation_errors_validate_registration( $this->validation->error_string, $eventID ) );
					}
					break;
			}
			
			die;
		}


		public function validate_event_release_check()
		{
			$this->load->model('proxies/Walk_proxy');
			$this->load->model('proxies/swamivisits/Swamivisits_proxy');
			
			//Get check info
			$curyear = $this->Walk_proxy->getCurrentYear();
			$RegIdArray = $this->Swamivisits_proxy->getEventUnpaidCheckRegId( $curyear );

			if (count($RegIdArray) > 0)
			{
				$flg = false;
				//CheckInfo
				for( $i = 0; $i < count($RegIdArray); $i++ )
				{
					$checknum = $this->input->post('svid'.$RegIdArray[$i]);
					if ($checknum != "")
					{
						$rules['svid'.$RegIdArray[$i]] = "trim|required|alpha_dash|min_length[1]|max_length[16]";
						$flg = true;
					}
				}
			
				if ($flg)
				{
					$this->validation->set_rules($rules);	
					$validationPassed = $this->validation->run();
				}
				else
					$validationPassed = true;
			}
			else
			{
				$validationPassed = true;
			}
			
			if($validationPassed)
			{
				echo( 'VALIDATION_SUCCESS' );
			}
			else
			{
				echo( $this->clean_validation_errors_validate_event_release_check( $this->validation->error_string, $RegIdArray ) );
			}
			
			die;
		}

		private function clean_validation_errors_validate_registration( $valErrors, $eventID )
		{
			switch ($eventID)
			{
				case 1:
					$valErrors = str_replace("firstname", "First name", $valErrors);
					$valErrors = str_replace("lastname", "Last name", $valErrors);
					$valErrors = str_replace("email", "Email", $valErrors);
					$valErrors = str_replace("numreg", "Number of participants", $valErrors);
					break;
			}
									
			return $valErrors;
		}		

		private function clean_validation_errors_validate_event_release_check( $valErrors, $RegIdArray )
		{

			if (count($RegIdArray) > 0)
			{
				//CheckInfo
				for( $i = 0; $i < count($RegIdArray); $i++ )
				{
					$checknum = $this->input->post('svid'.$RegIdArray[$i]);
					if ($checknum != "")
					{
						$valErrors = str_replace("'svid'.$RegIdArray[$i]", "Check #", $valErrors);
					}
				}					
			}
			
			return $valErrors;
		}
	}

?>